using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace QLThuHocPhi
{
    public partial class ListStudent : Form
    {
        QLThuHocPhi.Database.DBTool dbt = new QLThuHocPhi.Database.DBTool();
        DataSet ds = new DataSet();
        public ListStudent()
        {
            InitializeComponent();
            cbGioitinh.Text = "Nam";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SystemForm sf = new SystemForm();
            sf.Show();
            this.SetVisibleCore(false);
        }

        private void ListStudent_Load(object sender, EventArgs e)
        {
            ds = dbt.GetListSV("All","All");
            LoadData();      
            Loadcb();
        }

        private void Loadcb() {
            DataSet ds2 = new DataSet();
            ds2 = dbt.LoadTenKhoa();
            this.cbTenkhoa.DataSource = ds2.Tables["tenk"];
            this.cbTenkhoa.DisplayMember = "Tenkhoa";
            this.cbTenkhoa.ValueMember = "Makhoa";
            DataSet ds3 = new DataSet();
            ds3 = dbt.LoadKhoahoc();
            this.cbKhoahoc.DataSource = ds3.Tables["tenkhoa"];
            this.cbKhoahoc.DisplayMember = "Khoahoc";
            this.cbKhoahoc.ValueMember = "Makhoahoc";
            DataSet ds4 = new DataSet();
            ds4 = dbt.LoadTenkhoa1();
            this.cbkhoa2.DataSource = ds4.Tables[0];
            this.cbkhoa2.DisplayMember = "Tenkhoa";
            this.cbkhoa2.ValueMember = "Makhoa";
            ds = dbt.LoadKhoahoc1();
            this.cbkhoahoc1.DataSource = ds.Tables["Khoahoc1"];
            this.cbkhoahoc1.DisplayMember = "khoahoc";
            this.cbkhoahoc1.ValueMember = "Makhoahoc";
            ds = dbt.loaduutien();
            this.comboBox1.DataSource = ds.Tables["MucUuTien"];
            this.comboBox1.DisplayMember = "TenUuTien";
            this.comboBox1.ValueMember = "MaUuTien";
            comboBox1.SelectedItem = "Bình thường";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
          
            DateTime ngaysinh = DateTime.Parse(dateTimePicker1.Text);
            if (MessageBox.Show("Bạn có chắc chắn muốn thêm mức học phí này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                dbt.CreateSV(txtTenSV.Text, cbGioitinh.Text, ngaysinh, txtQuequan.Text, txtDiachi.Text, cbkhoa2.SelectedValue.ToString(), int.Parse(cbkhoahoc1.SelectedValue.ToString()), int.Parse(comboBox1.SelectedValue.ToString()));
            ds = dbt.GetListSV("All","All");
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
           // string ddv =txtkhoahoc.Text;
            string ngaysinh = dateTimePicker1.Value.ToShortDateString();
           // MessageBox.Show(dateTimePicker1.Value.ToShortDateString());
            //return;
            if (MessageBox.Show("Bạn có chắc chắn muốn sửa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                dbt.UpdateSV(int.Parse(txtMAsv.Text), txtTenSV.Text, cbGioitinh.Text, ngaysinh, txtQuequan.Text, txtDiachi.Text, cbkhoa2.SelectedValue.ToString(), int.Parse(cbkhoahoc1.SelectedValue.ToString()), int.Parse(comboBox1.SelectedValue.ToString()));
            ds = dbt.GetListSV("All","All");
            dataGridView1.DataSource = ds.Tables[0];
            txtTenSV.Text= "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                int index = dataGridView1.CurrentRow.Index;

                if (index != -1)
                {
                    string k = index.ToString();
                    dbt.DeleteSV(txtMAsv.Text);
                    dataGridView1.Rows.RemoveAt(index);
                    ds = dbt.GetListSV("All","All");
                    dataGridView1.DataSource = ds.Tables[0];
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = dataGridView1.CurrentRow.Index;
                txtMAsv.Text = (string)dataGridView1.Rows[i].Cells["MaSV"].Value.ToString();
                txtTenSV.Text = (string)dataGridView1.Rows[i].Cells["Ten"].Value.ToString();
                cbGioitinh.Text = (string)dataGridView1.Rows[i].Cells["GioiTinh"].Value.ToString();
                txtQuequan.Text = (string)dataGridView1.Rows[i].Cells["Quequan"].Value.ToString();
                txtDiachi.Text = (string)dataGridView1.Rows[i].Cells["DiaChi"].Value.ToString();
                // txtkhoahoc.Text = (string)dataGridView1.Rows[i].Cells["Khoahoc"].Value.ToString();
                 // txtNgaysinh.Text = (string)dataGridView1.Rows[i].Cells["NgaySinh"].Value.ToString();
                cbkhoa2.Text = (string)dataGridView1.Rows[i].Cells["Tenkhoa"].Value.ToString();
                cbkhoahoc1.Text = (string)dataGridView1.Rows[i].Cells["Khoahoc"].Value.ToString();
                comboBox1.Text = (string)dataGridView1.Rows[i].Cells["TenUuTien"].Value.ToString();
                dateTimePicker1.Text = (string)dataGridView1.Rows[i].Cells["NgaySinh"].Value.ToString();
            }
            catch (Exception ) { }
        }
        private void LoadData() {
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].HeaderText = "Mã sinh viên";
            dataGridView1.Columns[2].HeaderText = "Họ tên sinh viên";
            dataGridView1.Columns[1].HeaderText = "Giới tính";
            dataGridView1.Columns[3].HeaderText = "Ngày sinh";
            dataGridView1.Columns[4].HeaderText = "Quê quán";
            dataGridView1.Columns[5].HeaderText = "Địa Chỉ";
            dataGridView1.Columns[6].HeaderText = "Tên Khoa";
            dataGridView1.Columns[7].HeaderText = "Khóa học";
            dataGridView1.Columns[8].HeaderText = "Tên Ưu Tiên";
        }
        private void button5_Click(object sender, EventArgs e)
        {

            try
            {
                txttimkiem2.Text = "";
                cbTenkhoa.SelectedIndex = 0;
                cbKhoahoc.SelectedIndex = 0;
                string masv = txttimkiem.Text;
                if (masv.Length > 0) {
                    ds = dbt.TimkiemSV(masv);
                         LoadData();
                    
                }
                
            }
            catch (Exception ) { 
            
            }
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            txttimkiem.Text = "";
            try
            {
                string tensv = txttimkiem2.Text;
                if (tensv.Length == 0)
                {
                    string khoa = cbTenkhoa.Text;
                    string khoahoc = cbKhoahoc.Text;
                    ds = dbt.GetListSV(khoa, khoahoc);
                    LoadData();
                }
                if (tensv.Length > 0) {
                    string khoa = cbTenkhoa.Text;
                    string khoahoc = cbKhoahoc.Text;
                    ds = dbt.GetListSV(khoa, khoahoc,tensv);
                    LoadData();
                }
            }
            catch(Exception ) { }
        }
    }
}